﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MySQLManageData : System.Web.UI.Page
{
    private int perPage = 2;//每页记录个数
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            LoadData(0 * perPage, perPage);
            LBtnNext.PostBackUrl = "?page=2";
            if (Request.QueryString["action"] != null)
            {
                long id = Convert.ToInt64(Request.QueryString["id"]);
                switch (Request.QueryString["action"])
                {
                    case "edit":
                        UpdateInfo(id);
                        break;
                    case "delete":
                        DeleteInfo(id);
                        break;
                }
            }
        }
    }

    protected void LBtnNext_Click(object sender, EventArgs e)
    {
        int page = Convert.ToInt32(Request.QueryString["page"]);
        LoadData((page - 1) * perPage, perPage);
        LBtnNext.PostBackUrl = "?page=" + (page + 1);
    }

    //实现对记录的操作：新增/修改
    protected void BtnInfoSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Hfid.Value.Equals("-1"))//新增记录
            {
                AddInfo();
            }
            else
            {//修改记录
                SaveInfo();
            }
        }
        catch (Exception)
        {

            Literal1.Text = "数据处理异常！";
        }
    }

    private void LoadData(int start, int num)
    {
        try
        {
            //表头
            Literal1.Text = "<table>\n";
            Literal1.Text += "<tr>\n";
            Literal1.Text += "<th>内部序号</th>\n";
            Literal1.Text += "<th>标题</th>\n";
            Literal1.Text += "<th>内容</th>\n";
            Literal1.Text += "<th>发布时间</th>\n";
            Literal1.Text += "<th>作者</th>\n";
            Literal1.Text += "<th>查看次数</th>\n";
            Literal1.Text += "<th>分类</th>\n";
            Literal1.Text += "<th>&nbsp;</th>\n";
            Literal1.Text += "</tr>\n";
            //手动建立数据表格
            foreach (var data in infoDao.FindAll(start, num))
            {
                Literal1.Text += "<tr>\n";
                Literal1.Text += "<td>" + data.Id + "</td>\n";
                Literal1.Text += "<td>" + data.Title + "</td>\n";
                Literal1.Text += "<td>" + data.Content + "</td>\n";
                Literal1.Text += "<td>" + data.PublishTime + "</td>\n";
                Literal1.Text += "<td>" + data.Author + "</td>\n";
                Literal1.Text += "<td>" + data.Click_Number + "</td>\n";
                Literal1.Text += "<td>" + data.Category_Mark + "</td>\n";
                Literal1.Text += "<td>";
                Literal1.Text += "<a href =\"?action=edit&id=" + data.Id + "\">编辑</a>";
                Literal1.Text += "<a href =\"?action=delete&id=" + data.Id + "\">删除</a>";
                Literal1.Text += "</td>\n";
                Literal1.Text += "</tr>\n";
            }
            Literal1.Text += "</table>\n";

        }
        catch (Exception)
        {

            Literal1.Text = "数据加载异常！"; ;
        }
    }

    //实现对记录的操作：增加
    private void AddInfo()
    {
        info data = new info();
        data.Title = TbTitle.Text.Trim();
        data.Content = TbContent.Text.Trim();
        data.Author = TbAuthor.Text.Trim();
        data.PublishTime = DateTime.Now;
        data.Click_Number = 300;
        data.Category_Mark = "xyyw";//实际是应该从下拉列表控件获取数据
        long insertId = infoDao.Insert(data);
        Debug.WriteLine("插入数据返回ID: " + insertId);
    }
    //实现对记录的操作：更新--将要更新的内容读进来
    private void UpdateInfo(long id)
    {
        info data = infoDao.Find(id);
        TbTitle.Text = data.Title;
        TbContent.Text = data.Content;
        TbAuthor.Text = data.Author;
        Hfid.Value = data.Id.ToString();
    }
    //实现对记录的操作：更新--上传更新后的内容
    private void SaveInfo()
    {
        info data = infoDao.Find(Convert.ToInt64(Hfid.Value));
        data.Title = TbTitle.Text.Trim();
        data.Content = TbContent.Text.Trim();
        data.Author = TbAuthor.Text.Trim();
        infoDao.Update(data);
    }
    //实现对记录的操作：删除
    private void DeleteInfo(long id)
    {
        info data = infoDao.Find(id);
        infoDao.Delete(id);
    }

}